package com.pateo.springcloudgoods.web.controller;

import com.github.pagehelper.PageInfo;
import com.netflix.discovery.converters.Auto;
import com.pateo.springcloudgoods.dao.model.auto.AutoGoods;
import com.pateo.springcloudgoods.service.GoodsService;
import com.pateo.springcloudgoods.web.request.GoodsRequest;
import com.pateo.springcloudgoods.web.response.GoodsResponse;
import com.pateo.springcloudgoods.web.response.PageResponse;
import com.pateo.springcloudgoods.web.response.Response;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class GoodsController  implements com.pateo.springcloudgoods.web.GoodsService {

    @Autowired
    private GoodsService goodsService;

    @Override
    public Response addGoods(@Valid @RequestBody GoodsRequest request){
        AutoGoods autoGoods = new AutoGoods();
        BeanUtils.copyProperties(request,autoGoods);
        goodsService.insertGoods(autoGoods);
        return new Response();
    }

    @Override
    public Response updateGoods(@Valid @RequestBody GoodsRequest request){
        AutoGoods autoGoods = new AutoGoods();
        BeanUtils.copyProperties(request,autoGoods);
        int row = goodsService.updateGoods(autoGoods);
        return new Response();
    }

    public PageResponse<GoodsResponse> queryPage(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize){
        PageInfo<AutoGoods> pageInfo = goodsService.selectPage(pageNumber,pageSize);
        PageResponse<GoodsResponse> response = new PageResponse<>();
        if(pageInfo.getTotal()>0){
            response.setTotal(pageInfo.getTotal());
            response.setItems(new ArrayList<>());
            for(AutoGoods autoGoods : pageInfo.getList()){
                GoodsResponse goods = new GoodsResponse();
                BeanUtils.copyProperties(autoGoods,goods);
                response.getItems().add(goods);
            }
        }
        return response;
    }

    @Override
    public List<GoodsResponse> queryAll(){
        List<AutoGoods> list = goodsService.selectAll();
        List<GoodsResponse> responses = new ArrayList<>();
        if(!list.isEmpty()){
            for(AutoGoods ag : list){
                GoodsResponse goods = new GoodsResponse();
                BeanUtils.copyProperties(ag,goods);
                responses.add(goods);
            }
        }
        return responses;
    }

    @Override
    public GoodsResponse queryGoods(@PathVariable("id") Long id){
        AutoGoods autoGoods = goodsService.selectGoodsById(id);
        GoodsResponse response = new GoodsResponse();
        if(autoGoods != null){
            BeanUtils.copyProperties(autoGoods,response);
            return response;
        }
        return null;
    }

    @Override
    public Response deleteGoodsById(@PathVariable("id") Long id){
        goodsService.deleteGoods(id);
        return new Response();
    }

}
